﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.ProcessExecutor.Core.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DWF.Studio.ProcessExecutor.Core.Process
{
    /// <summary>
    /// 工程生成器
    /// </summary>
    internal class ProcessResolver
    {
        public ProcessInfo Resolve(string processDirectory)
        {
            var processFile = System.IO.Path.Combine(processDirectory, ".process");
            var processDetail = YamlHelper.Load<ProcessDetail>(processFile);
            var processInfo = new ProcessInfo
            {
                Id = processDetail.id,
                Name = processDetail.name,
                Description = processDetail.description,
                Ver = processDetail.ver,
                Reversion = processDetail.reversion,
                Platform = processDetail.platform,
                Start = System.IO.Path.Combine(processDirectory, "file", processDetail.start),
                ContainPackages = processDetail.containPackages,
                AllowParallel = processDetail.allowParallel,
                RequireAdmin = processDetail.requireAdmin,
                Directory = processDirectory
            };

            foreach (var item in processDetail.dependency)
            {
                Regex reg = new Regex("id=(.+);version=(.+);targetFramework=(.+)");//依赖信息
                Match match = reg.Match(item);
                string id = match.Groups[1].Value;
                string version = match.Groups[2].Value;
                string targetFramework = match.Groups[3].Value;

                processInfo.Dependency.Add(System.IO.Path.Combine(processDirectory, "activities", $"{id}.{version}", "lib", targetFramework));
            }
            processInfo.Dependency.Add(System.IO.Path.Combine(processDirectory, "libs"));

            foreach (var item in processDetail.file)
            {
                processInfo.File.Add(System.IO.Path.Combine(processDirectory, "file", item));
            }

            foreach (var item in processDetail.libs)
            {
                processInfo.Libs.Add(System.IO.Path.Combine(processDirectory, "libs", item));
            }

            foreach (var item in processDetail.images)
            {
                processInfo.Images.Add(System.IO.Path.Combine(processDirectory, "images", item));
            }

            foreach (var item in processDetail.res)
            {
                processInfo.Res.Add(System.IO.Path.Combine(processDirectory, "res", item));
            }

            return processInfo;
        }
    }
}
